package com.lysj.bhp.app.service;

import com.google.gson.Gson;
import com.lysj.bhp.app.base.AppContent;
import com.lysj.bhp.coinexchange.domain.Apply;
import com.lysj.bhp.coinexchange.domain.BhpcRecord;
import com.lysj.bhp.exchangerate.base.ExchangeRateContent;
import com.lysj.bhp.exchangerate.json.bcex.*;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 汇率相关服务类
 */
@Service
@Transactional
public class AppBuyBhpcService extends AppContent {

    private static Logger logger = LoggerFactory.getLogger(AppBuyBhpcService.class);


    public Resp buyBhpc(String email, BigDecimal amount, int walletType) throws Exception {

        if (amount.compareTo(BigDecimal.ZERO) != 1) {
            return new Resp(Resp.PARAM_ERROR, "金额输入不能小于0");
        }
        User user = userRepository.findByEmailAndStatus(email, 1);
        if (null == user) {
            return new Resp("转币错误");
        }

        //获得用户所有钱包
        BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(user.getUserId());
        EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(user.getUserId());
        BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(user.getUserId());
        //如果用户没创建bhpc钱包,则创建bhpc钱包

        if (ParamUtil.isBlack(bhpcWallet)) {
            return new Resp("转币失败");
        }

        BhpcRecord bhpcRecord = new BhpcRecord();
        //用户用比特币钱包兑换BHPC币
        if (walletType == 10) {
            if (ParamUtil.isBlack(bitCoinWallet)) {
                return new Resp(Resp.PARAM_ERROR, "金额不足,请先充值");
            }
            if (amount.compareTo(bitCoinWallet.getAvailBalance()) == 1) {
                return new Resp(Resp.PARAM_ERROR, "金额不足");
            }
            bhpcRecord.setExchangeRate(exchangeRate.getBITCOIN_TO_DOLLAR());
            bitCoinWallet.setBalance(bitCoinWallet.getBalance().subtract(amount));
            bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(amount));
            bitcoinWalletRepository.save(bitCoinWallet);
            bhpcRecord.setType(10);
        }
        //用户用以太坊钱包兑换BHPC币
        else if (walletType == 20) {
            if (ParamUtil.isBlack(ethereumWallet)) {
                return new Resp(Resp.PARAM_ERROR, "金额不足,请先充值");
            }
            if (amount.compareTo(ethereumWallet.getAvailBalance()) == 1) {
                return new Resp(Resp.PARAM_ERROR, "金额不足");
            }
            bhpcRecord.setExchangeRate(exchangeRate.getETHEREUM_TO_DOLLAR());
            ethereumWallet.setBalance(ethereumWallet.getBalance().subtract(amount));
            ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().subtract(amount));
            ethereumWalletRepository.save(ethereumWallet);
            bhpcRecord.setType(20);
        } else {
            return new Resp(Resp.PARAM_ERROR, "币种类型错误");
        }
        bhpcRecord.setBhpcToDollar(exchangeRate.getBHPC_TO_DOLLAR());
        bhpcRecord.setCreateDate(new Date());
        bhpcRecord.setUserId(user.getUserId());
        bhpcRecord.setWhitelist(0);
        bhpcRecord.setExchangeNum(amount);
        bhpcRecord.setOperationType(1); //APP端发起
        BigDecimal dollar = amount.multiply(bhpcRecord.getExchangeRate()).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal bigDecimal = dollar.divide(exchangeRate.getBHPC_TO_DOLLAR(), 6, BigDecimal.ROUND_HALF_UP);

        bhpcRecord.setBhpc(bigDecimal); //通过计算获得Bhpc的数量
        bhpcRecord.setStatus("成功");
        bhpcWallet.setBalance(bhpcWallet.getBalance().add(bigDecimal));
        bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(bigDecimal));
        bhpcWalletRepository.save(bhpcWallet);
        bhpcRecordRepository.save(bhpcRecord);
        logger.info(user.getEmail() + "->购买了:" + bigDecimal + " 个算力币!!");
        return new Resp("购买成功");
    }
}
